package com.wiz.common.util;

import java.math.BigInteger;
import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * 字节辅助工具
 * @author Hakeny
 */
public class ByteUtil {
	static {
		Security.addProvider(new BouncyCastleProvider());
	}
	
	/**
	 * 将value BCD化后放入长度为value.length/2 的buf中
	 * @param value
	 * @param buf
	 */
	public static void toBcd(String value, byte[] buf,int lr) {
		int charpos = 0; //char where we start
		int bufpos = 0;
		if (value.length() % 2 == 1) {
			value = lr==1 ? "0" + value : value + "0";
		}
		//encode the rest of the string
		while (charpos < value.length()) {
			buf[bufpos] = (byte) (((value.charAt(charpos) - 48) << 4) | (value.charAt(charpos + 1) - 48));
			charpos += 2;
			bufpos++;
		}
	}

	/**
	 * 将byte[]转为各种进制的字符串
	 * @param bytes byte[]
	 * @param radix 进制值,基数可以转换进制的范围，从Character.MIN_RADIX到Character.MAX_RADIX，超出范围后变为10进制
	 * @return 转换后的字符串
	 */
	public static String bytes2Integer(byte[] bytes, int radix) {
		return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
	}
	
	/**
	 * 将byte[]指定范围内转为各种进制的字符串
	 * @param bytes
	 * @param radix
	 * @param start
	 * @param length
	 * @return
	 */
	public static String bytes2Integer(byte[] bytes, int radix,int start,int length) {
		byte[] nb = new byte[length];
		for(int i=0;i<length;i++){
			nb[i]=bytes[start+i];
		}
		return new BigInteger(1,nb).toString(radix);// 这里的1代表正数
	}
	
	/**
     * 数字字符串转ASCII码字符串
     * @param String 字符串
     * @return ASCII字符串
     */
    public static String numToAsciiString(String content) {
        String result = "";
        int max = content.length();
        for (int i = 0; i < max; i++) {
            char c = content.charAt(i);
            String b = Integer.toHexString(c);
            result = result + b;
        }
        return result;
    }
    
    /**
     * 数字字符串转ASCII码字符串
     * @param String 字符串
     * @return ASCII字符串
     */
    public static String asciiToNum(String content) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < content.length(); i=i+2) {
        	String s = content.substring(i, i+2);
        	char r =(char)encodeHex2Integer(s);
            sb.append((char)r);
        }
        return sb.toString();
    }
    
    /**
     * 16进制转10进制
     * @param hex
     * @return
     */
	public static int encodeHex2Integer(String hex) { 
		return Integer.valueOf(hex,16);
	}
	
	/**
	 * bytes转化16进制字符串 采用不足2位，补0的做法
	 * @param byte[]
	 * @return String
	 */
	public static String Bytes2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}
	
	/**
	 * 将两个ASCII字符合成一个字节； 如："EF"--> 0xEF
	 * @param src0 byte
	 * @param src1 byte
	 * @return byte
	 */
	public static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
				.byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
				.byteValue();
		byte ret = (byte) (_b0 ^ _b1);
		return ret;
	}

	/**
	 * 将指定字符串src，以每两个字符分割转换为16进制形式 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF,0xD9}
	 * @param src String
	 * @return byte[]
	 */
	public static byte[] HexString2Bytes(String src) {
		byte[] ret = new byte[src.length() / 2];
		byte[] tmp = src.getBytes();
		for (int i = 0; i < tmp.length / 2; i++) {
			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
		}
		return ret;
	}
	
	public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
	}
}
